# 720  词典中最长的单词 每次遇到符合要求的单词一定是最长且字典序最小的单词  https://leetcode-cn.com/problems/longest-word-in-dictionary/
from typing import List


class Solution720:
    def longestWord(self, words: List[str]) -> str:
        # 按照长度递增,同样长度则递减
        words.sort(key=lambda x: (-len(x), x), reverse=True)
        longest = ""
        candidates = {""}
        for word in words:
            if word[:-1] in candidates:
                longest = word
                candidates.add(word)
        return longest
    # python中的静态方法和类方法  https://www.cnblogs.com/pinking/p/7944376.html
    @staticmethod
    def mainDemo():
        s = Solution720()
        words = ["a", "ae", "aea", "banana", "app", "appl", "ap", "apply", "apple"]
        print(s.longestWord(words))
